﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Net;
using System.IO;

namespace Dictionary_offline__tratu.soha.vn_
{
    public partial class frmEngVie :  Form //DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmEngVie()
        {
            InitializeComponent();
        }

        public String ListWord = "";
        public string wordIsShowing = "";

        public Font fontDefaut = new Font("Segoe UI", 10, FontStyle.Regular);
        public Font fontSearchWords = new Font("Segoe UI", 10, FontStyle.Bold);
        public Font fontEnglishWords = new Font("Segoe UI", 10, FontStyle.Italic);
        public Font fontPOS = new Font("Segoe UI", 11, FontStyle.Bold);
        public Font fontAcademics = new Font("Segoe UI", 12, FontStyle.Bold);
        public Font fontMeaning = new Font("Segoe UI", 10, FontStyle.Underline);
        public Color colorDefaut = Color.Black;
        public Color colorSearchWords = Color.Red;
        public Color colorEnglishWords = Color.Blue;
        public Color colorPOS = Color.Green;
        public Color colorAcademics = Color.OrangeRed;
        public Color colorMeaning = Color.Gray;

        private void comboBoxEx1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Back && cboSearchText.Focused == false)
            {
                cboSearchText.Focus();
            }
            return base.ProcessDialogKey(keyData);
        }

     /*   public void InsertTortbResult2(Color c, Font f, string s)
        {
            rtbResult2.SelectionFont = f;
            rtbResult2.SelectionBackColor = c;
            rtbResult2.AppendText(s);
        }
        */

        public Stream GetResult()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://tratu.soha.vn/dict/en_vn/" + cboSearchText.Text.ToLower());
            request.Method = "GET";
            request.ContentLength = 0;
            HttpWebResponse respone = (HttpWebResponse)request.GetResponse();
            return respone.GetResponseStream();
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                HtmlAgilityPack.HtmlDocument hdocMeaningType = new HtmlAgilityPack.HtmlDocument();
                HtmlAgilityPack.HtmlNodeCollection hdocSubMeaningType;
                HtmlAgilityPack.HtmlNodeCollection hdocSubMeaningExample;
                hdocMeaningType.Load(GetResult(), Encoding.UTF8);
                var result1 = hdocMeaningType.DocumentNode.SelectSingleNode("//h1[@class='firstHeading']/script");
                string tmp = result1.InnerText;
                tmp = tmp.Substring(tmp.IndexOf("=") + 2);
                tmp = tmp.Substring(0, tmp.IndexOf("'")).TrimEnd();
                if (tmp != "Kết quả tìm" && hdocMeaningType.DocumentNode.SelectSingleNode("//div[@class='section-h5']") != null)
                {
                    wordIsShowing = tmp.TrimEnd();
                    txtResult1.Text = tmp;
                    if (!ListWord.Contains(tmp + "#"))
                    {
                        cboSearchText.Items.Add(tmp);
                        ListWord += tmp + "#";
                    }
                    result1 = hdocMeaningType.DocumentNode.SelectSingleNode("//div[@class='section-h5']");
                    txtResult1.Text += "\r\n" + result1.InnerText.TrimStart();
                    rtbResult2.SelectionColor = Color.Black;
                    rtbResult2.Text = "123";
                    foreach (var item in hdocMeaningType.DocumentNode.SelectNodes("//div[@class='section-h2']"))
                    {
                        //xuất tên loại từ điển
                        string tam = "";
                        rtbResult2.SelectionColor = colorAcademics;
                        rtbResult2.SelectionFont = fontAcademics;
                        tam = item.SelectSingleNode("descendant::span[@class='mw-headline']").InnerText.TrimEnd();
                        rtbResult2.AppendText(tam);
                        rtbResult2.Text += "\n";
                        //lấy tên từ loại(danh động tính trạng) của từ bằng section 3
                        hdocSubMeaningType = item.SelectNodes("descendant::div[@class='section-h3']");
                        foreach (var subItem1 in hdocSubMeaningType)
                        {
                            //xuất tên từ loại
                            rtbResult2.SelectionColor = Color.Green;
                            rtbResult2.SelectionFont = fontPOS;
                            rtbResult2.AppendText(subItem1.SelectSingleNode("descendant::span[@class='mw-headline']").InnerText + "\n");
                            //trích xuất nghĩa trong section 5
                            foreach (var subItem2 in subItem1.SelectNodes("descendant::div[@class='section-h5']"))
                            { 
                                //Xuất tên nghĩa của từ
                                rtbResult2.SelectionColor = colorMeaning;
                                rtbResult2.SelectionFont= fontMeaning;
                                rtbResult2.AppendText(subItem2.SelectSingleNode("descendant::span[@class='mw-headline']").InnerText + "\n");
                                //Xuất ví dụ nếu có
                                if ((hdocSubMeaningExample = subItem2.SelectNodes("descendant::dl[@style='display: block;']")) != null)
                                {
                                    //InsertTortbResult2(colorEnglishWords, fontEnglishWords, hdocSubMeaningExample[0].InnerText + "\n");
                                }
                            }
                        }
                        rtbResult2.Text += "\r\n";
                        
                    }
                }
                else
                {
                    txtResult1.Text = "Not Avalable";
                    rtbResult2.Clear();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void cboSearchText_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((cboSearchText.Text.ToLower() != wordIsShowing.ToLower()))
            {
                btnSearch_Click(null, null);
            }
        }
    }
}